var falseNum = 0;
var rightNum = 0;
var rightlv = 0;
var appleNum = 2000;
var speed = 100;
var winNum = 50;
var loseNum = 50;
var id4 = 0;

var id1 = 0;
var id2 = 0;
var id3 = 0;
var id5 = 0;
window.onkeydown = function() {
	if(id5 == 1) {
		var key = event.keyCode;
	}
	var bg = document.getElementById("bg");
	for(i = 0; i < bg.children.length; i++) {
		var code = bg.children[i].getAttribute("keycode");
		if(key == code) {
			bg.removeChild(bg.children[i]);
			rightNum++;
			document.getElementById("right_score").innerHTML = rightNum;
			rightlv = (rightNum / (rightNum + falseNum)).toFixed(2) * 100;
			document.getElementById("right_parsent").innerHTML = rightlv;
			switch(rightNum) {
				case 1:
					allClear("smallapple6","block");
					break;
				case 10:
					allClear("smallapple5","block");
					break;
				case 15:
					allClear("smallapple4","block");
					break;
				case 20:
					allClear("smallapple3","block");
					break;
				case 25:
					allClear("smallapple2","block");
					break;
				case 30:
					allClear("smallapple1","block");
					break;
			}
			if(rightNum == winNum) {
				alert("恭喜通关");
				pause();
				cancelAll();
				id4 = 1;
			}
			break;
		}
	}
}

function exit() {
	if(confirm("是否退出")) {
		window.close()
	}
}
function allClear(idname,dis){
	document.getElementById(idname).style.display = dis;
}
function clear_apple() {
	allClear("smallapple1","none");
	allClear("smallapple2","none");
	allClear("smallapple3","none");
	allClear("smallapple4","none");
	allClear("smallapple5","none");
	allClear("smallapple6","none");
}

function shezhi() {
	allClear("shezhi_jiemian","block");
	document.getElementById("bg").innerHTML = " ";
	numTo0();
	allClear("shezhi_jiemian","block");
	pause();
	cancelAll();
	clear_apple();
}

function queding() {
	var gameLV = (document.getElementById("LVnum").value) * 1;
	winNum = document.getElementById("passNum").value;
	loseNum = document.getElementById("nopassNum").value;
	var nandu = document.getElementById("nandu");
	nandu.innerHTML = gameLV;
	switch(gameLV) {
		case 1:
			appleNum = 2000;
			speed = 100;
			break;
		case 2:
			appleNum = 1400;
			speed = 60;
			break;
		case 3:
			appleNum = 1000;
			speed = 30;
			break;
		case 4:
			appleNum = 600;
			speed = 10;
			break;
		default:
			alert("目前难度只有1到4");
			moren();
			nandu.innerHTML = 1;
			break;
			numTo0();
			cancelAll();

	}

//	console.log(gameLV + "/" + speed + "/" + appleNum)
	allClear("shezhi_jiemian","none");
}

function cancelAll() {
	rightNum = 0;
	falseNum = 0;
	document.getElementById("bg").innerHTML = "";
}

function quxiao() {
	allClear("shezhi_jiemian","none");
}
//           	 
function moren() {
	document.getElementById("LVnum").value = "1";
	document.getElementById("passNum").value = "50";
	document.getElementById("nopassNum").value = "50";
}

function over() {
	history.go(0);
}

function pause() {
	clearInterval(id2);
	clearInterval(id1);
	id2 = 0;
	id1 = 0;
	id5 = 0;
}

function start() {
	if(id1 == 0 && id2 == 0 && id3 == 0) {
		id2 = setInterval("addapple()", appleNum);
		id1 = setInterval("appledown()", speed);
		if(id4 == 1) {
			numTo0();
			id4 = 0;
			clear_apple();
		}
		id5 = 1;
	}
}

function numTo0() {
	document.getElementById("right_score").innerHTML = "0";
	document.getElementById("false_score").innerHTML = "0";
	document.getElementById("right_parsent").innerHTML = "0";
}

function appledown() {
	var bg = document.getElementById("bg")
	for(i = 0; i < bg.children.length; i++) {
		var t = parseInt(bg.children[i].style.top);
		t += 5;
		bg.children[i].style.top = t + "px";
		if(t > 390) {
			bg.children[i].setAttribute("class", "badapple");
			bg.children[i].innerHTML = " ";
		}
		if(t > 410) {
			bg.removeChild(bg.children[i]);
			falseNum++;
			document.getElementById("false_score").innerHTML = falseNum;
			rightlv = (rightNum / (rightNum + falseNum)).toFixed(2) * 100;
			document.getElementById("right_parsent").innerHTML = rightlv;
			if(falseNum == loseNum) {
				alert("闯关失败");
				pause();
				cancelAll();
				id4 = 1;
				clear_apple();
			}
		}
	}
}

function addapple() {
//	console.log("add");
	var a = document.createElement("div")
	var num = parseInt(Math.random() * 26) + 65;
	var l = parseInt(Math.random() * 700);
	a.innerHTML = "&#" + num + ";";
	a.setAttribute("class", "apple");
	a.setAttribute("keycode", num);
	a.style.left = l + "px";
	a.style.top = "5px";
	document.getElementById("bg").appendChild(a);
}